package com.tietiao.common.util;

import javax.servlet.http.HttpServletRequest;

public class HttpServletUtil {
	/**
	 * 获取真实的远程客户端IP，不受Nginx和apache等前端webserver分发影响
	 * @param request
	 * @return
	 * @author 
	 */
	public static String getRealRemoteAddr(HttpServletRequest request) {
		if (request ==null)
			return null;

		String ip = request.getHeader("X-Real-IP");//该值为运维配置项
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("x-forwarded-for");
		}
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

}
